﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using Castle.ActiveRecord;
using System.Web.UI;
using System.IO;
using SimplePMT.Data;
using SimplePMT.Components;
using NHibernate.Expression;

namespace SimplePMT.Control
{
    public partial class ReportProjectStatusControl : System.Web.UI.UserControl
    {
        public string FromDate = string.Empty;
        public string ToDate = string.Empty;
        public string[] projectList;
        public int TaskContainerID;
        public string projectPlan = string.Empty;
        public string project = string.Empty;
        public Dictionary<int, decimal> hourMap;

        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        public override string ToString()
        {
            
            if (TaskContainerID > 0)
            {
                projectPlan = GetProjectTreeNodeAll(TaskContainerID);
            }
            string result = "";
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            this.Render(htmlWriter);
            result = writer.ToString();
            return result;
        }

        private string GetProjectTreeNodeAll(int parentContainerID)
        {
            StringBuilder output = new StringBuilder();
            TaskContainer c = TaskContainer.Find(parentContainerID);
            if (c.ID > 0)
            {
                project = c.Label; ;
            }
            output.Append(GetProjectTreeNode(parentContainerID, true));
            return output.ToString();
        }

        private string GetProjectTreeNode(int parentContainerID, bool showExpanded)
        {
            bool ReadOnly = false;
            if (HttpContext.Current.Session["MemberID"] != null)
            {
                Member member = Member.Find(Convert.ToInt32(HttpContext.Current.Session["MemberID"]));
                ReadOnly = member.ReadOnly;
            }
            StringBuilder projectJsArray = new StringBuilder();
            StringBuilder output = new StringBuilder();
            using (new SessionScope())
            {
                IList<TaskContainer> containerList = TaskContainer.FindAll(Expression.Eq("ParentContainerID", parentContainerID), Expression.Eq("IncludeInProjectPlan", true));
                output.Append("<table width=100% cellspacing=0 style=\"border:dotted 1px #869AAE\">\r\n");

                foreach (TaskContainer container in containerList)
                {
                    decimal hoursActual = 0;
                    int itemCount = 0;
                    StringBuilder taskOutput = new StringBuilder();

                    foreach (TaskContainerItem item in container.Items)
                    {
                        decimal taskHours = 0;
                        bool show = false;
                        Task task = Task.Find(item.TaskID);
                        if (task.DueDate >= Convert.ToDateTime(FromDate) && task.DueDate <= Convert.ToDateTime(ToDate))
                        {
                            show = true;
                        }
                        TaskNote[] notes = TaskNote.FindAllByProperty("Task", Task.Find(item.TaskID));
                        foreach (TaskNote note in notes)
                        {
                            taskHours += note.Hours;
                        }
                        string taskIcon = task.TaskType.Icon;
                        bool closed = !task.ClosedDateTime.ToShortDateString().Equals("1/1/1900");

                        if (closed && task.ClosedDateTime >= Convert.ToDateTime(FromDate))
                        {
                            show = true;
                        }
                        if (closed)
                        {
                            taskIcon = "checkmark.png";
                        }
                        if (show)
                        {
                            taskOutput.Append("<tr id=\"taskRow" + task.ID + "\" onmouseover=\"this.className='highlightRow';\" onmouseout=\"this.className='';\" OnClick=\"openModal('TaskView.aspx?id=" + task.ID + "&cid=" + item.TaskContainer.ID + "', 'SimplePMT - " + task.Title.Replace("'", "\\'").Replace("\"", "") + "');\"");

                            taskOutput.Append(">\r\n");
                            taskOutput.Append("<td colspan=2 height=17 id=\"taskid" + task.ID + "\"");
                            if (closed)
                            {
                                taskOutput.Append(" style=\"text-decoration:line-through;\"");
                            }

                            taskOutput.Append("><img src=\"images/" + taskIcon + "\"> " + taskHours + " Hours -  <b>" + task.Title + "</b> (");

                            if (task.AssignedMemberID > 0)
                            {
                                Member member = Member.Find(task.AssignedMemberID);
                                taskOutput.Append(member.FirstName + " " + member.LastName + " " + task.DueDate.Value.ToShortDateString());
                            }
                            else
                            {
                                taskOutput.Append("unassigned");
                            }
                            taskOutput.Append(") " + task.Priority.Label + " - " + task.TaskType.Label);
                            taskOutput.Append("</td>");
                            taskOutput.Append("</tr>\r\n");

                        }
                        hoursActual += taskHours;
                    }
                    output.Append("<tr onmouseover=\"this.className='highlightRow';\" onmouseout=\"this.className='';\">\r\n");
                    output.Append("<td height=17 align=right OnClick=\"javascript:OnClick=loadTree(" + container.ID + ", document.img" + container.ID + ");\">\r\n");
                    output.Append("<img hspace=3 vspace=3 src=\"images/");
                    if (showExpanded)
                    {
                        output.Append("open.gif");
                    }
                    else
                    {
                        output.Append("closed.gif");
                    }
                    output.Append("\" name=\"img" + container.ID + "\" border=0/>&nbsp;");

                    output.Append("</td>\r\n");
                    output.Append("<td style=\"white-space:nowrap;\">\r\n");
                    if (itemCount > 0)
                    {
                        output.Append("<span style=\"white-space:nowrap;\" OnClick=\"javascript:OnClick=loadTree(" + container.ID + ", document.img" + container.ID + ");\">\r\n");
                    }
                    output.Append(container.Label + "</span>&nbsp;&nbsp;<b>"+ hoursActual +"</b> Hours Total");
                    
                    output.Append("</span></td>\r\n");
                    output.Append("</tr>\r\n");
                    output.Append("<tr>\r\n");
                    output.Append("<td width=19 align=right></td>\r\n");
                    output.Append("<td id=\"container" + container.ID + "\"");
                    if (!showExpanded)
                    {
                        output.Append(" style=\"display:none\"");
                    }
                    output.Append(">\r\n");
                    if (showExpanded)
                    {
                        output.Append(GetProjectTreeNode(container.ID, showExpanded));
                    }
                    output.Append("</td>\r\n");
                    output.Append("</tr>\r\n");
                    output.Append("<tr>\r\n");
                    output.Append("<td></td>\r\n");
                    output.Append("<td id=\"taskList" + container.ID + "\"");
                    if (!showExpanded)
                    {
                        output.Append(" style=\"display:none\"");
                    }
                    output.Append(">");
                    if (container.Attachments.Count > 0 || container.Items.Count > 0)
                    {
                        output.Append("<table width=100% cellspacing=0 cellpadding=2 border=0>\r\n");
                    }

                    output.Append(taskOutput.ToString());
                    taskOutput.Remove(0, taskOutput.Length);
                    

                    if (container.Attachments.Count > 0 || container.Items.Count > 0)
                    {
                        output.Append("</table>");
                    }
                    output.Append("</td></tr>\r\n");
                }
                output.Append("</table>\r\n");
            }
            return output.ToString();
        }
        
    }
}